package com.sptpc.company.service.impl;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sptpc.company.common.Const;
import com.sptpc.company.common.ServerResponse;
import com.sptpc.company.pojo.User;
import com.sptpc.company.service.IUserService;
import com.sptpc.company.util.MD5Util;
import com.sptpc.company.dao.UserMapper;

@Service("iUserService")
public class UserServiceImpl implements IUserService{
	
	@Autowired
	private UserMapper userMapper;

	public ServerResponse login(String username, String password) {
		// TODO Auto-generated method stub
		int count = userMapper.checkUserName(username);
		if(count == 0) {
			return ServerResponse.createByErrorMessage("用户名不存在");
		}
		
		String md5Password = MD5Util.MD5EncodeUtf8(password);
		System.out.println(md5Password);
		User user = userMapper.selectLogin(username,md5Password);
		if(user == null) {
			return ServerResponse.createByErrorMessage("密码错误");
		}
		user.setPassword(StringUtils.EMPTY);
		
		return ServerResponse.createBySuccess("登录成功", user);
	}

	public ServerResponse<String> register(User user) {
		// TODO Auto-generated method stub
		int count = userMapper.checkUserName(user.getUsername());
		if(count > 0) {
			return ServerResponse.createByErrorMessage("用户名已存在");
		}
		count = userMapper.checkEmail(user.getEmail());
		if(count > 0) {
			return ServerResponse.createByErrorMessage("邮箱已存在");
		}
		user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
		count = userMapper.insertRegister(user);
		if(count == 0) {
			return ServerResponse.createByErrorMessage("注册失败");
		}
		return ServerResponse.createBySuccessMessage("注册成功");
	}

}
